package com.yuncheng.spcyApi.service.impl;

import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.vo.wjmb.WjmbJcxmTitleVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.entity.SpcyRsWjmb;
import com.yuncheng.spcyApi.mapper.SpcyRsWjmbMapper;
import com.yuncheng.spcyApi.service.ISpcyRsWjmbService;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 审评查验-文件模板
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcyRsWjmbServiceImpl extends BaseServiceImpl<SpcyRsWjmbMapper, SpcyRsWjmb> implements ISpcyRsWjmbService {

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private SpcyRsWjmbMapper spcyRsWjmbMapper;

    @Override
    public String getTemplateRealAddress(String fywflcode,String templateType) {
        List<SpcyRsWjmb> fileTemplateList = list(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(StringUtils.isNotBlank(fywflcode),SpcyRsWjmb::getFywflcode, fywflcode)
                .eq(StringUtils.isNotBlank(templateType),SpcyRsWjmb::getFmblx1, templateType));

        // 该领域下的模板类型
        if (CollectionUtils.isEmpty(fileTemplateList)){
            return "";
        }
        // 获取第一个模板
        SpcyRsWjmb oneTemplate = fileTemplateList.get(0);

        // 文件路径为空
        if (StringUtils.isEmpty(oneTemplate.getFcflj())){
            return "";
        }
        // 模板路径
        String templateUrl = "";
        templateUrl = businessConstant.UPLOAD_WJMB_PATH + oneTemplate.getFcflj();
        return templateUrl;
    }

    @Override
    public String selectFmblx1ById(String id) {
        return Optional.ofNullable(spcyRsWjmbMapper.selectFmblx1ById(id)).orElseGet(()->"");
    }

    @Override
    public WjmbJcxmTitleVo selectJcxmTitleByFsqsxId(String sqsxid) {
        List<WjmbJcxmTitleVo> beanList = spcyRsWjmbMapper.selectJcxmTitleByFsqsxId(sqsxid);
        if (CollectionUtils.isEmpty(beanList)){
            WjmbJcxmTitleVo bean = new WjmbJcxmTitleVo();

            bean.setFmsmbbt(SpcyCommonConstant.WJMB_MSMBNR_TITLE);
            bean.setFjcxmbt(SpcyCommonConstant.WJMB_JCXM_TITLE);

            return bean;
        }

        WjmbJcxmTitleVo bean = beanList.get(0);
        if (StringUtils.isBlank(bean.getFmsmbbt())){
            bean.setFmsmbbt(SpcyCommonConstant.WJMB_MSMBNR_TITLE);
        }
        if (StringUtils.isBlank(bean.getFjcxmbt())){
            bean.setFjcxmbt(SpcyCommonConstant.WJMB_JCXM_TITLE);
        }

        return bean;

    }
}
